﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class LaySubWaterCatcher : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				if (commandData == null)
				{
					throw new System.ArgumentNullException();
				}
				UIApplication application = commandData.Application;
				UIDocument activeUIDocument = application.ActiveUIDocument;
				Document document = activeUIDocument.Document;
				Selection selection = activeUIDocument.Selection;
				List<ViewType> list = new List<ViewType>();
                list.Add((ViewType)1);
                list.Add((ViewType)2);
                list.Add((ViewType)4);
				if (!YJKRevitTools.ViewCheck(application.ActiveUIDocument, list))
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					for (;;)
					{
						HYEquipmentCommon hyequipmentCommon = new HYEquipmentCommon();
						ICollection<ElementId> allFamilyInstance = hyequipmentCommon.GetAllFamilyInstance(document, BuiltInCategory.OST_MechanicalEquipment);
						SpaceFamilyDataReader spaceFamilyDataReader = new SpaceFamilyDataReader();
						FormLaySubWaterCatcher formLaySubWaterCatcher = new FormLaySubWaterCatcher();
						DialogResult dialogResult = formLaySubWaterCatcher.ShowDialog();
						if (dialogResult == DialogResult.OK)
						{
							FamilySymbol familySymbolById = spaceFamilyDataReader.GetFamilySymbolById(formLaySubWaterCatcher.id, "SubWaterCatcher", document, BuiltInCategory.OST_MechanicalEquipment);
							if (familySymbolById == null)
							{
								YJKMessageBox.Information("没有获取到该型号的族");
							}
							else
							{
								if (familySymbolById != null)
								{
									try
									{
										activeUIDocument.PromptForFamilyInstancePlacement(familySymbolById);
									}
									catch (Exception)
									{
									}
								}
								ICollection<ElementId> allFamilyInstance2 = hyequipmentCommon.GetAllFamilyInstance(document, BuiltInCategory.OST_MechanicalEquipment);
								new List<ElementId>();
								foreach (ElementId item in allFamilyInstance.Intersect(allFamilyInstance2))
								{
									allFamilyInstance2.Remove(item);
								}
								transaction = new Transaction(document, "布置分集水器");
								transaction.Start();
								foreach (ElementId elementId in allFamilyInstance2)
								{
									SetParaForInstance.SetOffsetForInstance(document.GetElementById(elementId), formLaySubWaterCatcher.offset, document);
								}
								document.Regenerate();
								transaction.Commit();
							}
						}
						else if (dialogResult == DialogResult.Cancel)
						{
							break;
						}
					}
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			return result;
		}
	}
}
